package com.hiv.mapper;

import com.hiv.common.param.page.PageQueryAdminParam;
import com.hiv.mapper.domain.Admin;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 管理员Mapper接口
 *
 * @author zhanghong
 * @date 2022-07-09
 */
@Mapper
@Repository
public interface AdminMapper {
    Admin selectById(Long id);
    List<Admin> selectList(Admin admin);
    int insert(Admin admin);
    int update(Admin admin);
    int deleteById(Long id);
    int deleteByIds(String[] ids);
    @Select({"select * from admin where id = #{id} "})
    Admin findById(@Param("id") long id);
    @Select({"select * from admin where account = #{account} limit 1"})
    Admin findByAccount(@Param("account") String account);
    @Delete("delete from admin where id = #{id} limit 1")
    void delete(@Param("id") long id);
    @Select({"select * from admin where account = #{account} and password = #{password} limit 1"})
    Admin findByCondition(@Param("account") String account, @Param("password") String password);

    @Select({"<script>",
            " select count(1) from admin inner join admin_role on admin.id=admin_role.admin_id inner join role on admin_role.role_id=role.id",
            "   <where>",
            "       admin.id!=1",//排除超级管理员
            "       <if test=\"param.adminId != null\">and admin.id!=#{param.adminId}</if>",//排除当前登录的管理员
            "       <if test=\"param.account != null and param.account != ''\">and admin.account like concat('%',#{param.account},'%')</if> ",
            "       <if test=\"param.name != null and param.name != ''\">and admin.name like concat('%',#{param.name},'%')</if> ",
            "       <if test=\"param.hospitalPid != null\">and admin.hospital_pid=#{param.hospitalPid}</if>",
            "       <if test=\"param.hospitalId != null\">and admin.hospital_id=#{param.hospitalId}</if>",
            "       <if test=\"param.type != null\">and admin.type=#{param.type}</if>",
            "       <if test=\"param.type == null\">and admin.type is not null</if>",
            "       <if test=\"param.roleId != null\">and role.id=#{param.roleId}</if>",
            "       <if test=\"param.platform != null\">and role.platform=#{param.platform}</if>",
            "   </where>",
            "</script>"})
    long superPageCount(@Param("param") PageQueryAdminParam param);
    @Select({"<script>",
            " select admin.*,role.platform from admin inner join admin_role on admin.id=admin_role.admin_id inner join role on admin_role.role_id=role.id",
            "   <where>",
            "       admin.id!=1",//排除超级管理员
            "       <if test=\"param.adminId != null\">and admin.id!=#{param.adminId}</if>",//排除当前登录的管理员
            "       <if test=\"param.account != null and param.account != ''\">and admin.account like concat('%',#{param.account},'%')</if> ",
            "       <if test=\"param.name != null and param.name != ''\">and admin.name like concat('%',#{param.name},'%')</if> ",
            "       <if test=\"param.hospitalPid != null\">and admin.hospital_pid=#{param.hospitalPid}</if>",
            "       <if test=\"param.hospitalId != null\">and admin.hospital_id=#{param.hospitalId}</if>",
            "       <if test=\"param.type != null\">and admin.type=#{param.type}</if>",
            "       <if test=\"param.type == null\">and admin.type is not null</if>",
            "       <if test=\"param.roleId != null\">and role.id=#{param.roleId}</if>",
            "       <if test=\"param.platform != null\">and role.platform=#{param.platform}</if>",
            "   </where>",
            "   limit #{limit} offset #{offset}",
            "</script>"})
    List<Admin> superPageQuery(@Param("param") PageQueryAdminParam param, @Param("limit") int limit, @Param("offset") long offset);

    @Select({"<script>",
            " select count(1) from admin",
            "   <where>",
            "       admin.id!=1",//排除超级管理员
            "       <if test=\"param.adminId != null\">and admin.id!=#{param.adminId}</if>",//排除当前登录的管理员
            "       <if test=\"param.account != null and param.account != ''\">and admin.account like concat('%',#{param.account},'%')</if> ",
            "       <if test=\"param.name != null and param.name != ''\">and admin.name like concat('%',#{param.name},'%')</if> ",
            "       <if test=\"param.hospitalPid != null\">and admin.hospital_pid=#{param.hospitalPid}</if>",
            "       <if test=\"param.hospitalId != null\">and admin.hospital_id=#{param.hospitalId}</if>",
            "       <if test=\"param.type != null\">and admin.type=#{param.type}</if>",
            "       <if test=\"param.roleId != null\">and role.id=#{param.roleId}</if>",
            "       <if test=\"param.platform != null\">and role.platform=#{param.platform}</if>",
            "   </where>",
            "</script>"})
    long subPageCount(@Param("param") PageQueryAdminParam param);
    @Select({"<script>",
            " select admin.* from admin",
            "   <where>",
            "       admin.id!=1",//排除超级管理员
            "       <if test=\"param.adminId != null\">and admin.id!=#{param.adminId}</if>",//排除当前登录的管理员
            "       <if test=\"param.account != null and param.account != ''\">and admin.account like concat('%',#{param.account},'%')</if> ",
            "       <if test=\"param.name != null and param.name != ''\">and admin.name like concat('%',#{param.name},'%')</if> ",
            "       <if test=\"param.hospitalPid != null\">and admin.hospital_pid=#{param.hospitalPid}</if>",
            "       <if test=\"param.hospitalId != null\">and admin.hospital_id=#{param.hospitalId}</if>",
            "       <if test=\"param.type != null\">and admin.type=#{param.type}</if>",
            "       <if test=\"param.roleId != null\">and role.id=#{param.roleId}</if>",
            "       <if test=\"param.platform != null\">and role.platform=#{param.platform}</if>",
            "   </where>",
            "   limit #{limit} offset #{offset}",
            "</script>"})
    List<Admin> subPageQuery(@Param("param") PageQueryAdminParam param, @Param("limit") int limit, @Param("offset") long offset);

    @Select("select hospital_id from admin where type = 2")
    List<Long> listAllHospitalId();
}
